package br.com.arosa.dao;

import java.util.*;
import java.sql.*;
import br.com.easynet.database.DataSet;
import br.com.jdragon.dao.*;
import br.com.arosa.transfer.*;

public class Tb_item_compraDAO extends ObjectDAO { 
	 public Tb_item_compraDAO(DAOFactory dao) throws Exception {
		 setDAOFactory(dao);
 		 con = dao.create();
 	 }

	 public void insert(Tb_item_compraT tb_item_compraT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "insert into tb_item_compra  ( id_compra, id_grade, valor_unitario, quantidade, valor_total) values ( ? , ? , ? , ? , ? )";
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_item_compraT.getId_compra());
			 pStmt.setObject(2, tb_item_compraT.getId_grade());
			 pStmt.setObject(3, tb_item_compraT.getValor_unitario());
			 pStmt.setObject(4, tb_item_compraT.getQuantidade());
			 pStmt.setObject(5, tb_item_compraT.getValor_total());
			 pStmt.execute();
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public void update(Tb_item_compraT tb_item_compraT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "update tb_item_compra set  id_grade=?, valor_unitario=?, quantidade=?, valor_total=?  where  id_compra=?";
 			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_item_compraT.getId_grade());
			 pStmt.setObject(2, tb_item_compraT.getValor_unitario());
			 pStmt.setObject(3, tb_item_compraT.getQuantidade());
			 pStmt.setObject(4, tb_item_compraT.getValor_total());
			 pStmt.setObject(5, tb_item_compraT.getId_compra());
			 pStmt.execute();
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public void delete(Tb_item_compraT tb_item_compraT ) throws Exception { 
		 PreparedStatement pStmt=null;
		 try {
			 String sql = "delete from tb_item_compra where  id_compra=?";
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_item_compraT.getId_compra());
			 pStmt.execute();
		 } catch (Exception e)  {
 			  throw e;
 		} finally {
 			 try { 
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 private List<Tb_item_compraT> resultSetToObjectTransfer(ResultSet rs) throws Exception { 
		 List<Tb_item_compraT> objs = new Vector();
		 while (rs.next()) { 
 			 Tb_item_compraT tb_item_compraT = new Tb_item_compraT();
			 tb_item_compraT.setId_compra(rs.getInt("id_compra"));
			 tb_item_compraT.setId_grade(rs.getInt("id_grade"));
			 tb_item_compraT.setValor_unitario(rs.getDouble("valor_unitario"));
			 tb_item_compraT.setQuantidade(rs.getDouble("quantidade"));
			 tb_item_compraT.setValor_total(rs.getDouble("valor_total"));
			 objs.add(tb_item_compraT);
 		 }
 		 return objs; 
 	 }
	 public List<Tb_item_compraT> getAll() throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_item_compra"; 
			 pStmt = con.prepareStatement(sql);
			 rs = pStmt.executeQuery();
 			 List<Tb_item_compraT> list = resultSetToObjectTransfer(rs);
 			 return list; 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_item_compraT> getById(Tb_item_compraT tb_item_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_item_compra where  id_compra=?"; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_item_compraT.getId_compra());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_item_compraT> getById_compra(Tb_item_compraT tb_item_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_item_compra where  id_compra = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_item_compraT.getId_compra());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_item_compraT> getById_grade(Tb_item_compraT tb_item_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_item_compra where  id_grade = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_item_compraT.getId_grade());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_item_compraT> getByValor_unitario(Tb_item_compraT tb_item_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_item_compra where  valor_unitario = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_item_compraT.getValor_unitario());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_item_compraT> getByQuantidade(Tb_item_compraT tb_item_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_item_compra where  quantidade = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_item_compraT.getQuantidade());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 	 public List<Tb_item_compraT> getByValor_total(Tb_item_compraT tb_item_compraT) throws Exception { 
		 PreparedStatement pStmt=null;
		 ResultSet rs=null;
		 try {
			 String sql = "select * from tb_item_compra where  valor_total = ? "; 
			 pStmt = con.prepareStatement(sql);
			 pStmt.setObject(1, tb_item_compraT.getValor_total());
			 rs = pStmt.executeQuery();
 			 return resultSetToObjectTransfer(rs); 
 		 } catch (Exception e) {
 			  throw e;
 		} finally {
 			 try { rs.close();
 			 pStmt.close();} catch (Exception e) {}
 			  
 		} 
 	} 

 

 }